package com.ag.zhh.action;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.multipart.FilePart;
import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Scope("prototype")
@Controller("UploadFaxFileAction")
public class UploadFaxFileAction extends ActionSupport {

	static final long serialVersionUID = -4848248679889814408L;

	private List<File> Filedata;
	private List<String> FiledataFileName;
	private List<String> FiledataContentType;
	private String name;
	private String taskid; 

	public String doUpload() {
		System.out.println("========doupload=====");
		taskid = UUID.randomUUID().toString();
		ActionContext ac = ActionContext.getContext();
		HttpServletRequest request = ServletActionContext.getRequest();
		System.out.println("====="+request.getAttribute("name")+"=====");
		System.out.println("====="+request.getParameter("name")+"=====");
		ServletContext sc = (ServletContext) ac
				.get(ServletActionContext.SERVLET_CONTEXT);
		String savePath = sc.getRealPath("/");
		System.out.println("===savePath=="+savePath+"=====");
		savePath = savePath + "uploads/";
		File f1 = new File(savePath);
		if (!f1.exists()) {
			f1.mkdirs();
		}
		int size = Filedata.size();
		if (size == 0)
			return ERROR;
		String extName = null;
		String name = null;
		
		System.out.println("===FiledataFileName=="+FiledataFileName+"=====");
		System.out.println("===FiledataContentType=="+FiledataContentType+"=====");
		for (int i = 0; i < size; i++) {
			extName = FiledataFileName.get(i).substring(
					FiledataFileName.get(i).lastIndexOf("."));
			name = UUID.randomUUID().toString();
			File file = new File(savePath + name + extName);
			try {
				FileUtils.copyFile(Filedata.get(i), file);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			System.out.println("-----------------------realfilename:"+name + extName);
			System.out.println("-----------------------taskid:"+taskid);
			response.getWriter().print(name + extName+","+taskid);    //返回随机生成的文件名和任务id随机数
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
		try{
			System.out.println("-----------------------后台往cti服务器传送文件");
			String url ="http://172.17.7.177/vasptools/service/v1/filemanager/upload?output=json&type=sendfax&subtype=subdir&subdir=.";
			PostMethod filePost = new PostMethod(url);
			List<String> fileNames = new ArrayList<String>();
			ArrayList<FilePart> parts = new ArrayList<FilePart>();
			File f=new File(savePath + name + extName);    //把上传到服务器的文件传到cti服务器
			fileNames.add(f.getAbsolutePath());
			if (fileNames.size() != 0) {
				String fileName = null;
				File file = null;
				for (int i = 0; i < fileNames.size(); i++) {
					fileName = fileNames.get(i);
					file = new File(fileName);					
						parts.add(new FilePart(file.getName(), file));					
				}
			}
			if (parts.size() != 0) {
				filePost.setRequestEntity(new MultipartRequestEntity(parts.toArray(new FilePart[parts.size()]),filePost.getParams()));
				HttpClient client = new HttpClient();
				client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
				int status;
					status = client.executeMethod(filePost);				
				if (status == HttpStatus.SC_OK) {
					System.out.println("--------------上传成功ok");
				}else{
					System.out.println("--------------上传失败off");
				}
				}
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (HttpException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		return null;
	}

	public List<File> getFiledata() {
		return Filedata;
	}

	public void setFiledata(List<File> filedata) {
		Filedata = filedata;
	}

	public List<String> getFiledataFileName() {
		return FiledataFileName;
	}

	public void setFiledataFileName(List<String> filedataFileName) {
		FiledataFileName = filedataFileName;
	}

	public List getFiledataContentType() {
		return FiledataContentType;
	}

	public void setFiledataContentType(List<String> filedataContentType) {
		FiledataContentType = filedataContentType;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getTaskid() {
		return taskid;
	}

	public void setTaskid(String taskid) {
		this.taskid = taskid;
	}
	
	
}
